08. Parts of a For Loop

Parts of a For Loop

Walking through a For Loop

The for loop explicitly forces you to define the start point, stop point, and each step of the loop. In fact, you'll get an Uncaught SyntaxError: Unexpected token ) if you leave out any of the three required pieces.

for ( start; stop; step ) {
  // do this thing
}

Here's an example of a for loop that prints out the values from 0 to 5. Notice the semicolons separating the different statements of the for loop: var i = 0; i < 6; i = i + 1

for (var i = 0; i < 6; i = i + 1) {
  console.log("Printing out i = " + i);
}

Prints:

Printing out i = 0

Printing out i = 1

Printing out i = 2

Printing out i = 3

Printing out i = 4

Printing out i = 5